package com.example.a12.activity;

import androidx.appcompat.app.AppCompatActivity;
import androidx.appcompat.widget.AppCompatButton;

import android.content.Intent;
import android.os.Bundle;
import android.widget.EditText;
import android.widget.TextView;
import android.widget.Toast;

import com.example.a12.MainActivity;
import com.example.a12.R;
import com.example.a12.utils.MySQLiteOpenHelper;

public class LoginActivity extends AppCompatActivity {
    // 登录页功能实现
    private EditText username, password;
    private AppCompatButton buttonLogin,buttonRegister;
    private TextView update;
    private MySQLiteOpenHelper mySQLiteOpenHelper;
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_login);
        username = findViewById(R.id.account);
        password = findViewById(R.id.password);
        buttonLogin = findViewById(R.id.loginBase);
        buttonRegister = findViewById(R.id.registerBase);
        update=findViewById(R.id.update);
        mySQLiteOpenHelper = new MySQLiteOpenHelper(this);

        buttonLogin.setOnClickListener(view -> {
            String name = username.getText().toString();
            String pass = password.getText().toString();
            boolean loginSuccess = mySQLiteOpenHelper.login(name, pass);
            if (loginSuccess) {
                Toast.makeText(this, "恭喜，登陆成功！",
                        Toast.LENGTH_SHORT).show();
                Intent intentHome = new Intent(this, MainActivity.class);
                startActivity(intentHome);
            } else {
                Toast.makeText(this, "用户名或密码错误，请重新输入！",
                        Toast.LENGTH_SHORT).show();
            }

        });
        buttonRegister.setOnClickListener(view -> {
            Intent intentRegister = new Intent(this, RegisterActivity.class);
            startActivity(intentRegister);
        });
        update.setOnClickListener(view -> {
            Intent intentRegister = new Intent(this, UpdateActivity.class);
            startActivity(intentRegister);
        });

    }

}